/*
 * Copyright (c) 2022 Kirky
 * Licensed under the Open Software Licence, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/osl-3.0.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lingeringdream.luciole.helper.list;

import com.lingeringdream.luciole.ResourceTable;
import com.lingeringdream.luciole.helper.ResourceHelper;
import com.lingeringdream.luciole.model.entity.Classification;
import com.lingeringdream.luciole.slice.DetailSlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * 类别列表项提供者
 *
 * @author Kirky
 * @version 1.0
 * @date 2022/5/3
 */
public class ClassResultProvider extends BaseItemProvider {
    private List<Classification> classes;
    private final AbilitySlice slice;
    private boolean isChinese;

    public ClassResultProvider(AbilitySlice abilitySlice, List<Classification> classes, boolean isChinese) {
        this.slice = abilitySlice;
        if (classes != null) {
            this.classes = classes;
        } else {
            this.classes = new ArrayList<>(0);
        }
        this.isChinese = isChinese;
    }

    public void addItem(Classification classification) {
        if (classification != null) {
            this.classes.add(classification);
        }
    }

    @Override
    public int getCount() {
        return classes.size();
    }

    @Override
    public Object getItem(int position) {
        return classes.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        final Component component;
        if (convertComponent == null) {
            component = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_layout_class_item, null, false);
        } else {
            component = convertComponent;
        }

        if (classes.size() == 0) {
            return null;
        }
        Classification classification = classes.get(position);
        Text title = component.findComponentById(ResourceTable.Id_classItem_title);
        if (isChinese) {
            title.setText(classification.getcName());
        } else {
            title.setText(classification.geteName());
        }

        Text clazz = component.findComponentById(ResourceTable.Id_classItem_class);
        if (classification.getClazz() == 0) {
            clazz.setText(ResourceHelper.getString(slice.getContext(), ResourceTable.String_recyclable, "可回收垃圾", null));
            clazz.setTextColor(ResourceHelper.getColor(slice.getContext(), ResourceTable.Color_recyclable, 0xFF00FFFF));
        } else if (classification.getClazz() == 1) {
            clazz.setText(ResourceHelper.getString(slice.getContext(), ResourceTable.String_food, "厨余垃圾", null));
            clazz.setTextColor(ResourceHelper.getColor(slice.getContext(), ResourceTable.Color_food, 0xFF2BDE14));
        } else if (classification.getClazz() == 2) {
            clazz.setText(ResourceHelper.getString(slice.getContext(), ResourceTable.String_other, "其他垃圾", null));
            clazz.setTextColor(ResourceHelper.getColor(slice.getContext(), ResourceTable.Color_other, 0xFF91A6AB));
        } else if (classification.getClazz() == 3) {
            clazz.setText(ResourceHelper.getString(slice.getContext(), ResourceTable.String_harmful, "有害垃圾", null));
            clazz.setTextColor(ResourceHelper.getColor(slice.getContext(), ResourceTable.Color_harmful, 0xFFFE0000));
        }

        //        设置监听
        component.setClickedListener(component1 -> {
//            跳转详情页
            Intent intent = new Intent();
            intent.setParam("cid", classification.getCid());
            slice.present(new DetailSlice(), intent);
        });

        return component;
    }
}
